package com.sicau.basedemo.controller;


import com.github.dozermapper.core.Mapper;
import com.sicau.basedemo.common.constant.UrlConstants;
import com.sicau.basedemo.common.returning.ResultData;
import com.sicau.basedemo.config.aop.ValidateAspect;
import com.sicau.basedemo.entity.param.newsType.NewsTypeAddParam;
import com.sicau.basedemo.entity.param.newsType.PracticeTypeUpdateParam;
import com.sicau.basedemo.entity.po.NewType;
import com.sicau.basedemo.service.NewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;

/**
 * <p>
 * 新闻类型表 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@Api(tags = "新闻类型相关接口")
@RequestMapping(UrlConstants.PRACTICE_TYPE_PREFIX)
public class NewsTypeController {

    @Resource
    private NewsTypeService newsTypeService;

    @Resource
    private Mapper dozerMapper;

    @PostMapping
    @ApiOperation("添加一个新闻类型")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> addPracticeType(@RequestBody @Valid NewsTypeAddParam newsTypeAddParam,
                                              @ApiParam(hidden = true)
                                              @SuppressWarnings("unused") BindingResult bindingResult) {

        NewType newType = dozerMapper.map(newsTypeAddParam, NewType.class);
        return newsTypeService.save(newType)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED();
    }

    @DeleteMapping
    @ApiOperation("删除一个或多个新闻类型")
    public ResultData<Object> removePrizes(@RequestParam("ids") ArrayList<Integer> ids) {
        return newsTypeService.removeByIds(ids)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED().setResultIns("部分或全部数据删除失败, 可能有的数据已经被删除了");
    }

    @PutMapping("/{id}")
    @ApiOperation("修改新闻类型")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> updatePrize(@PathVariable String id,
                                          @RequestBody @Valid PracticeTypeUpdateParam practiceTypeUpdateParam,
                                          @ApiParam(hidden = true)
                                          @SuppressWarnings("unused") BindingResult bindingResult) {
        //todo
        return null;
    }
}

